/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CapEvents;

import AreaDeDesenho.PainelDeDesenho;
import Dialogos.ParamAngulo;
import Dialogos.ParamXY;
import Formas.Forma;
import Helpers.ETransformacoes;
import Helpers.Transformer;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

/**
 *
 * @author Pedro
 */
public class EventosTranformacao implements ActionListener, MenuListener {

    PainelDeDesenho pane;
    
    public EventosTranformacao(PainelDeDesenho pane)
    {
        this.pane = pane;
    
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
       if(e.getSource() instanceof JMenuItem)
       {
            Transformer transf = new Transformer();
            transf.setTransformacao(ETransformacoes.None);
            JMenuItem source = (JMenuItem)e.getSource();
            if(source.getText().equals("Girar"))
            {                
                ParamAngulo p = new ParamAngulo(null);            
                if(p.isOk())
                {
                    transf.setAngulo(p.retornaRadianos());
                    transf.setTransformacao(ETransformacoes.Rotate);
                }
           }
            else if(source.getText().equals("Escalar"))
            {
             
                ParamXY p = new ParamXY(null);            
                if(p.isOk())
                {
                    transf.setX(p.getXV());
                    transf.setY(p.getYV());
                    transf.setTransformacao(ETransformacoes.Scale);
                }
            }
            else if(source.getText().equals("Transladar"))
            {
                ParamXY p = new ParamXY(null);
                if(p.isOk())
                {
                    transf.setX(p.getXV());
                    transf.setY(p.getYV());
                    transf.setTransformacao(ETransformacoes.Translate);
                }
            }
            else if(source.getText().equals("Deformar"))
            {
                ParamXY p = new ParamXY(null);
                if(p.isOk())
                {
                    transf.setX(p.getXV());
                    transf.setY(p.getYV());
                    transf.setTransformacao(ETransformacoes.Sheare);
                }
            }
            else if(source.getText().equals("Desfazer"))
                this.pane.refazer.push(this.pane.filaTransf.pop());
            else if(source.getText().equals("Refazer") && this.pane.refazer.size() > 0)
                this.pane.filaTransf.push(this.pane.refazer.pop());

            if(transf.getTransformacao() != ETransformacoes.None)
                this.pane.filaTransf.push(transf);

            this.pane.repaint();
        }
    }

    @Override
    public void menuSelected(MenuEvent e) {
           JMenu menu = (JMenu)e.getSource();
            for(Component j: menu.getMenuComponents())
                 if(j instanceof JMenuItem)
                      if(((JMenuItem)j).getText().equals("Refazer"))
                            j.setEnabled(this.pane.refazer.size() > 0);
                      else if(((JMenuItem)j).getText().equals("Desfazer"))
                            j.setEnabled(this.pane.filaTransf.size() > 1);

    }

    @Override
    public void menuDeselected(MenuEvent e) {

    }

    @Override
    public void menuCanceled(MenuEvent e) {

    }

}
